########## Background info #############

## This code replicates all background and contextual figures/tables that feature in the main text

## Load required packages

rm(list=ls())

library(tidyverse)
library(gridExtra)
library(tmap)
library(sf)
library(kableExtra)

########### FIGURE 1 main text ############

### VDem data and plot

vdem_data <- readRDS("vdem_plot_data.rds")

ggplot(vdem_data, 
                aes(x=Year, y=estimate, 
                    group = variable, col=variable, fill=variable)) +
  geom_ribbon(aes(ymin=lower, ymax=upper), alpha=0.7) +
  geom_line(col="black") +
  theme_minimal() +
  scale_fill_manual(values = c("darkorange", "lightblue", 
                               "darkgreen", "darkolivegreen3", 
                               "grey50")) +
  scale_colour_manual(values = c("darkorange", "lightblue", 
                                 "darkgreen", "darkolivegreen3", 
                                 "grey50")) +
  theme(legend.position = "bottom", 
        legend.title = element_blank(), 
        axis.title.x = element_blank()) +
  scale_y_continuous(limits = c(0,1)) +
  scale_x_continuous(limits = c(1994, 2019), 
                     breaks = c(1994, 1999, 2004, 2009, 2014, 2019)) +
  ggtitle("a) Malawian democracy represents global averages") +
  labs(subtitle = "Sources: V-Dem electoral democracy index (1994-2019)") +
  ylab("Electoral democracy index (0-1)")


### ITU data and plot

itu_plot_data <- readRDS("itu_plot_data.rds")

ggplot(itu_plot_data, aes(x=Year, y=share, col=Entity), group=Entity) +
  geom_point(alpha=0.7) +
  geom_line() +
  theme_bw() +
  scale_colour_manual(values = c("darkorange", "darkblue")) +
  scale_y_continuous(limits = c(0,26.5),
                     breaks = c(0,5,10,15,20,25),
                     labels = c("0%", "5%", "10%", "15%", "20%", "25%")) +
  scale_x_continuous(breaks = c(1999, 2004, 2009, 2014, 2019)) +
  theme(legend.title = element_blank(), 
        legend.position = "bottom", 
        axis.title.x = element_blank()) +
  ylab("% population with access") +
  labs(title = "b) Internet coverage in Malawi has historically lagged its neighbours", 
       subtitle = "Source: International Telecommunication Union (1999-2019)")


### Combine plots (as in text)

grid.arrange(vdem_plot, itu_plot)


############# FIGURE 2 main text #############

### Polling stations data and plot

stations <- readRDS("stations.rds")
adm0 <- readRDS("adm0.rds")

ps_plot1 <- ggplot(adm0) +
  geom_sf(fill="white", col="black") +
  geom_sf(data=stations, size=0.1, alpha=0.25, col="black") +
  ggtitle("a) Polling station locations") +
  theme_classic() +
  theme(axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.x = element_blank(),
        axis.text.y = element_blank(), 
        axis.ticks.x = element_blank(), 
        axis.ticks.y = element_blank(), 
        axis.line.x = element_blank(), 
        axis.line.y = element_blank())

### Election results data and plot

ps2019_clean <- readRDS("ps2019_clean.rds")

ps_plot2 <- ggplot(ps2019_clean, aes(x=dpp_share*100)) +
  geom_histogram(fill="lightgrey", col="black", 
                 bins = 100) +
  geom_vline(xintercept = mean(ps2019_clean$dpp_share*100, na.rm=T),
             col="darkred", size=1) +
  theme_minimal() +
  xlab("Share of DPP votes at polling station (%)") + ylab("Count") +
  ggtitle("b) Incumbent vote share")

ps_plot3 <- ggplot(ps2019_clean, aes(x=rejected_share*100)) +
  geom_histogram(fill="lightgrey", col="black", 
                 bins = 100) +
  scale_x_continuous(limits=c(0,30)) +
  geom_vline(xintercept = mean(ps2019_clean$rejected_share*100, na.rm=T),
             col="darkred", size=1) +
  theme_minimal() +
  xlab("Share of rejected ballots at polling station (%)") + ylab("Count") +
  ggtitle("c) Ballot rejection rate")

### Combine into one figure

grid.arrange(arrangeGrob(ps_plot1), 
             arrangeGrob(ps_plot2, ps_plot3, ncol=1), 
             ncol=2, widths=c(1,1))


######### TABLE 1 main text ########

rm(list=ls())
ps_inside_codes_2019 <- readRDS("ps_inside_codes_2019.rds")

ps_inside_codes_2019 <- ps_inside_codes %>%
  select(inside2014, inside2016, inside2017, total) %>%
  rename(total_votes_cast = total) %>%
  mutate(X = 1:4912) %>%
  select(inside2014, inside2016, inside2017, total_votes_cast)

coverage_means <- ps_inside_codes_2019 %>%
  mutate(inside2014 = as.numeric(inside2014),
         inside2016 = as.numeric(inside2016),
         inside2017 = as.numeric(inside2017)) %>%
  summarise(mean_2014 = mean(inside2014, na.rm=T)*100,
            mean_2016 = mean(inside2016, na.rm=T)*100,
            mean_2017 = mean(inside2017, na.rm=T)*100) %>%
  t() %>% data.frame() %>%
  rename(`Proportion (%)` = '.') %>%
  mutate(Year = c("Dec 2014", "Dec 2016", "Dec 2017"))

coverage_counts <- ps_inside_codes_2019 %>%
  mutate(inside2014 = as.numeric(inside2014),
         inside2016 = as.numeric(inside2016),
         inside2017 = as.numeric(inside2017)) %>%
  summarise(count_2014 = sum(inside2014, na.rm=T),
            count_2016 = sum(inside2016, na.rm=T),
            count_2017 = sum(inside2017, na.rm=T)) %>%
  t() %>% data.frame() %>%
  rename(`Count` = '.') %>%
  mutate(year = c(2014, 2016, 2017))

new_voters <- ps_inside_codes_2019 %>%
  mutate(vote2014 = as.numeric(inside2014)*total_votes_cast,
         vote2016 = as.numeric(inside2016)*total_votes_cast,
         vote2017 = as.numeric(inside2017)*total_votes_cast) %>%
  summarise(mean_2014_vote = sum(vote2014, na.rm=T),
            mean_2016_vote = sum(vote2016, na.rm=T),
            mean_2017_vote = sum(vote2017, na.rm=T)) %>%
  t() %>% data.frame() %>%
  rename(`Count` = '.') %>%
  mutate(Year = c("Dec 2014", "Dec 2016", "Dec 2017"), 
         new_voter = Count - lag(Count), 
         new_voter = ifelse(is.na(new_voter) == TRUE, "", new_voter))


coverage_desc <- coverage_means %>%
  mutate(`Polling stations` = coverage_counts$`Count`) %>%
  mutate(`Increase (%)` = ((`Proportion (%)`-lag(`Proportion (%)`))/lag(`Proportion (%)`))*100) %>%
  dplyr::select(Year, `Polling stations`, `Increase (%)`) %>%
  mutate(`Increase (%)` = as.numeric(`Increase (%)`),
         `Increase (%)` = round(`Increase (%)`, 2),
         `Increase (%)` = ifelse(is.na(`Increase (%)`) == TRUE, "", `Increase (%)`),
         `Added votes cast (2019)` = new_voters$new_voter)

rownames(coverage_desc) <- NULL

kable(coverage_desc, 
      caption = "3G coverage provision in Malawi",
      align = "c",
      booktabs=T, 
      digits=1) %>%
  row_spec(c(0), bold=T) %>%
  kable_styling(latex_options = "HOLD_position", 
                font_size = 10)



